﻿using System;
using System.Data.SqlClient;

using Daco.Pattern;

namespace Daco.Security
{
    namespace Providers
    {
        using Daco.Data;

        public class CRoleProvider
        {
            private CDbAccess _db = null;

            private static CRoleProvider _instance = null;
            public static CRoleProvider Instance
            {
                get
                {
                    if (_instance == null)
                        _instance = CPageSingleton<CRoleProvider>.Instance;
                    return _instance;
                }
            }

            public CRoleProvider()
            {
                _db = new CDbAccess(Core.CBaseConfig.Current.DbSchema, "Roles_");
                _db.SetConnectionManagerAdapter<CRoleProvider>();
            }
            //
            // Summary:
            //     Gets an array of user names in a role where the user roomID contains the specified
            //     user roomID to match.
            //
            // Parameters:
            //   roleName:
            //     The role to search in.
            //
            //   usernameToMatch:
            //     The user roomID to search for.
            //
            // Returns:
            //     A string array containing the names of all the users where the user roomID
            //     matches usernameToMatch and the user is a member of the specified role.
            public string[] FindUsersInRole(string roleName, string usernameToMatch)
            {
                SqlCommand cmd = new SqlCommand(_db.GetObjectName(""));
                cmd.CommandType = System.Data.CommandType.StoredProcedure;

                cmd.Parameters.Add("@RoleName", System.Data.SqlDbType.NVarChar, 256).Value = roleName;
                cmd.Parameters.Add("@UserNameToMatch", System.Data.SqlDbType.NVarChar, 50).Value = usernameToMatch;

                string[] arrNames = { };

                try
                {

                }
                finally { }
                return arrNames;
            }
            //
            // Summary:
            //     Gets a list of all the roles for the configured applicationName.
            //
            // Returns:
            //     A string array containing the names of all the roles stored in the data source
            //     for the configured applicationName.
            public string[] GetAllRoles()
            {
                SqlCommand cmd = new SqlCommand(_db.GetObjectName("GetAllRoles"));
                cmd.CommandType = System.Data.CommandType.StoredProcedure;

                System.Collections.Generic.List<string> list = new System.Collections.Generic.List<string>();

                using (CDbConnectionManager m = _db.GetConnectionManager())
                {
                    cmd.Connection = m.OpenDBConnection;
                    try
                    {
                        System.Data.IDataReader reader = _db.ExecuteReader(cmd, System.Data.CommandBehavior.SingleResult);
                        while (reader.Read())
                            list.Add(reader["RoleName"].ToString());
                        reader.Close();
                    }
                    finally { }
                }
                return list.ToArray();
            }
            //
            // Summary:
            //     Gets a list of the roles that a specified user is in for the configured applicationName.
            //
            // Parameters:
            //   username:
            //     The user to return a list of roles for.
            //
            // Returns:
            //     A string array containing the names of all the roles that the specified user
            //     is in for the configured applicationName.
            public string[] GetRolesForUser(string username)
            {
                System.Collections.Generic.List<string> list = new System.Collections.Generic.List<string>();

                string oldObjectQualifier = _db.GetObjectQualifier();

                _db.SetObjectQualifier("UsersInRoles_");
                SqlCommand cmd = new SqlCommand(_db.GetObjectName("GetRolesForUser"));
                cmd.CommandType = System.Data.CommandType.StoredProcedure;

                cmd.Parameters.Add("@UserName", System.Data.SqlDbType.NVarChar, 256).Value = username;

                using (CDbConnectionManager m = _db.GetConnectionManager())
                {
                    cmd.Connection = m.OpenDBConnection;
                    System.Data.IDataReader reader = _db.ExecuteReader(cmd, System.Data.CommandBehavior.SingleResult);
                    while (reader.Read())
                        list.Add(reader["RoleName"].ToString());
                    reader.Close();
                }

                _db.SetObjectQualifier(oldObjectQualifier);
                return list.ToArray();
            }
            //
            // Summary:
            //     Gets a list of users in the specified role for the configured applicationName.
            //
            // Parameters:
            //   roleName:
            //     The roomID of the role to get the list of users for.
            //
            // Returns:
            //     A string array containing the names of all the users who are members of the
            //     specified role for the configured applicationName.
            public string[] GetUsersInRole(string roleName)
            {
                SqlCommand cmd = new SqlCommand(_db.GetObjectName("GetUsersInRole"));
                cmd.CommandType = System.Data.CommandType.StoredProcedure;

                string[] arrNames = { };

                try
                {

                }
                finally { }
                return arrNames;
            }
            //
            // Summary:
            //     Gets a value indicating whether the specified user is in the specified role
            //     for the configured applicationName.
            //
            // Parameters:
            //   username:
            //     The user roomID to search for.
            //
            //   roleName:
            //     The role to search in.
            //
            // Returns:
            //     true if the specified user is in the specified role for the configured applicationName;
            //     otherwise, false.
            public bool IsUserInRole(string username, string roleName)
            {
                string oldObjectQualifier = _db.GetObjectQualifier();

                _db.SetObjectQualifier("UsersInRoles_");
                SqlCommand cmd = new SqlCommand(_db.GetObjectName("IsUserInRole"));
                cmd.CommandType = System.Data.CommandType.StoredProcedure;

                cmd.Parameters.Add("@UserName", System.Data.SqlDbType.NVarChar, 50).Value = username;
                cmd.Parameters.Add("@RoleName", System.Data.SqlDbType.NVarChar, 256).Value = roleName;
                cmd.Parameters.Add("@RETURN_VALUE", System.Data.SqlDbType.Int).Direction = System.Data.ParameterDirection.ReturnValue;

                _db.ExecuteNonQuery(cmd);
                int retv = Convert.ToInt32(cmd.Parameters["@RETURN_VALUE"].Value);

                _db.SetObjectQualifier(oldObjectQualifier);
                return retv > 0;
            }

            //
            // Summary:
            //     Gets a value indicating whether the specified role roomID already exists in
            //     the role data source for the configured applicationName.
            //
            // Parameters:
            //   roleName:
            //     The roomID of the role to search for in the data source.
            //
            // Returns:
            //     true if the role roomID already exists in the data source for the configured
            //     applicationName; otherwise, false.
            public bool RoleExists(string roleName)
            {
                return false;
            }
        }
    }
}
